﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

public class WaitInput : MonoBehaviour
{
    public KeyCode target;
    [Tooltip("委托执行多次，高频率输入请勾选，并将委托OnInput")]
    public bool isUpdate = false;
    
    [Space]
    
    public UnityEvent onInput;
    public UnityEvent onInputDown;
    public UnityEvent onInputUp;
    
    public WaitInputGroup group;

    public bool IsGroupLock { get; set; } = false;

    bool isInvoke = false;

    private void OnEnable()
    {
        isInvoke = false;
    }

    private void Update()
    {
        bool flag = false;
        if (!IsGroupLock && Input.GetKey(target) && !isInvoke)
        {
            InvokeInput();
            flag = true;
        }
        if (Input.GetKeyDown(target) && !isInvoke)
        {
            if (group)
            {
                group.DownInput(this);
            }
            InvokeDown();
            flag = true;
        }
        if (Input.GetKeyUp(target) && !isInvoke)
        {
            if (group)
            {
                group.UpInput(this);
            }
            if (IsGroupLock)
            {
                IsGroupLock = false;
            }
            else
            {
                InvokeUp();
                flag = true;
            }
        }
        if (flag && !isUpdate)
        {
            isInvoke = true;
        }
    }

    public void InvokeInput()
    {
        onInput?.Invoke();
    }

    public void InvokeDown()
    {
        onInputDown?.Invoke();
    }

    public void InvokeUp()
    {
        onInputUp?.Invoke();
    }
}
